import meow.Authority
import meow.Account

class BootStrap {
  def authenticateService

  def init = {servletContext ->
    if (Authority.count() == 0) {
      // 初期データ投入:Authority(ROLE_ADMIN)は必須
      new Authority(authority: "ROLE_ADMIN", description: "").save(flush: true)
      def roleUser = new Authority(authority: "ROLE_USER", description: "").save(flush: true)

      if (Account.count() == 0) {
        // デモユーザアカウント投入:これは無くても良い
        def demo1 = new Account(username: "demouser", userRealName: "デモユーザ",
                                passwd: authenticateService.encodePassword("password")).save()
        def demo2 = new Account(username: "hogeuser", userRealName: "ほげユーザ",
                                passwd: authenticateService.encodePassword("password")).save()
        roleUser.addToPeople(demo1)
        roleUser.addToPeople(demo2)
        (1..10).each {
          def account = new Account(username: "user${it}", userRealName: "ほげユーザ ${it}",
                                    passwd: authenticateService.encodePassword("password")).save()
          roleUser.addToPeople(account)
        }
        roleUser.save()
      }
    }
  }
  def destroy = {
  }
} 